/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.vertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

import java.util.List;

/**
 * @author <a href="mailto:hedyn@foxmail.com">HeDYn</a>
 */
public class Deployment {
    
    private final Class<? extends AbstractVerticle> verticleClass;
    private final DeploymentOptions deploymentOptions;
    
    protected Deployment(Class<? extends AbstractVerticle> verticleClass) {
        this.verticleClass = verticleClass;
        this.deploymentOptions = new DeploymentOptions();
    }
    
    public Deployment setWorker(boolean worker) {
        deploymentOptions.setWorker(worker);
        return this;
    }
    
    public Deployment setWorkerPoolName(String workerPoolName) {
        deploymentOptions.setWorkerPoolName(workerPoolName);
        return this;
    }
    
    public Deployment setWorkerPoolSize(int workerPoolSize) {
        deploymentOptions.setWorkerPoolSize(workerPoolSize);
        return this;
    }
    
    public Deployment setMaxWorkerExecuteTime(long maxWorkerExecuteTime) {
        deploymentOptions.setMaxWorkerExecuteTime(maxWorkerExecuteTime);
        return this;
    }
    
    public Deployment setInstances(int instances) {
        deploymentOptions.setInstances(instances);
        return this;
    }
    
    public Deployment setHa(boolean ha) {
        deploymentOptions.setHa(ha);
        return this;
    }
    
    public Deployment setExtraClasspath(List<String> extraClasspath) {
        deploymentOptions.setExtraClasspath(extraClasspath);
        return this;
    }
    
    public Deployment setMultiThreaded(boolean multiThreaded) {
        deploymentOptions.setMultiThreaded(multiThreaded);
        return this;
    }
    
    public Deployment setIsolationGroup(String isolationGroup) {
        deploymentOptions.setIsolationGroup(isolationGroup);
        return this;
    }
    
    public Deployment setIsolatedClasses(List<String> isolatedClasses) {
        deploymentOptions.setIsolatedClasses(isolatedClasses);
        return this;
    }
    
    public Deployment setConfig(JsonObject config) {
        deploymentOptions.setConfig(config);
        return this;
    }

    void deploy(Vertx vertx) {
        deploy(vertx, null);
    }

    void deploy(Vertx vertx, Handler<AsyncResult<String>> completionHandler) {
        vertx.deployVerticle(verticleClass.getName(), deploymentOptions, completionHandler);
    }

    void deploy(String prefix, Vertx vertx) {
        deploy(prefix, vertx, null);
    }

    void deploy(String prefix, Vertx vertx, Handler<AsyncResult<String>> completionHandler) {
        String name = prefix + ":" + verticleClass.getName();
        vertx.deployVerticle(name, deploymentOptions, completionHandler);
    }

    public static Deployment verticle(Class<? extends AbstractVerticle> verticleClass) {
        return new Deployment(verticleClass);
    }

    public static Deployment worker(Class<? extends AbstractVerticle> verticleClass) {
        return new Deployment(verticleClass).setWorker(true);
    }
}
